package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.dto.MomentsCommentsDTO;
import com.qiangqiang.task.server.entity.MomentsComments;
import com.qiangqiang.task.server.mapper.MomentsCommentsMapper;
import com.qiangqiang.task.server.mapper.MomentsMapper;
import com.qiangqiang.task.server.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-01-06 17:32
 **/
@Service
public class MomentsCommentsService extends BaseService {
    @Autowired
    private MomentsCommentsMapper momentsCommentsMapper;

    private SnowflakeIdWorker momentsCommentsWorker = new SnowflakeIdWorker(0, 7);

    public boolean addMomentsComments(MomentsComments momentsComments) {
        momentsComments.setMomentsCommentId(momentsCommentsWorker.nextId());
        momentsComments.setCreateTime(Instant.now().toEpochMilli());
        momentsComments.setUpdateTime(Instant.now().toEpochMilli());
        return momentsCommentsMapper.insertMomentsComments(momentsComments) > 0;
    }

    public List<MomentsCommentsDTO> findAllCommentsByMomentsId(long momentsId) {

        return momentsCommentsMapper.queryAllCommentsByMomentsId(momentsId)
                .stream()
                .map(this::beanToDTO)
                .collect(Collectors.toList());
    }
}
